#include "movingobject.h"
#include "../../SageVis/dynamicvalue.h"

MovingObject::MovingObject(QGraphicsItem *parent) :
    QGraphicsWidget(parent)
{
    isTargeting = false;

    dynX = new DynamicValue( 0.0 );
    dynY = new DynamicValue( 0.0 ); //init 0, no need to store anything yet
    dynWidth = new DynamicValue( 50.0 );
    dynHeight = new DynamicValue( 50.0 );
}

MovingObject::~MovingObject()
{
    delete dynX;
    delete dynY;
    delete dynWidth;
    delete dynHeight;
}

void MovingObject::target(float x, float y)
{
    isTargeting = true;
    dynX->setValue( pos().x() );
    dynY->setValue(pos().y() );
    dynX->setTarget( x );
    dynY->setTarget( y );
}

void MovingObject::advance()
{
    if( !isTargeting )
        return;

    if( dynX->isTargeting() )
    {
        dynX->update();
        setPos( dynX->value, pos().y() );
    }

    if( dynY->isTargeting() )
    {
        dynY->update();
        setPos( pos().x(), dynY->value );
    }

    isTargeting = dynX->isTargeting() || dynY->isTargeting()
                    || dynWidth->isTargeting() || dynHeight->isTargeting() ;
}

QRectF MovingObject::boundingRect() const
{
    return QRectF(-1.0*dynWidth->value/2.0, -1.0*dynHeight->value/2.0, dynWidth->value, dynHeight->value );
}

QPainterPath MovingObject::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void MovingObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    painter->setBrush( Qt::blue );
    painter->drawEllipse( boundingRect() );
}

QRectF centerXCenterYRect()
{
    return QRectF(-1.0*dynWidth->value/2.0, -1.0*dynHeight->value/2.0, dynWidth->value, dynHeight->value );
}

QRectF centerXTopYRect()
{
    return QRectF(-1.0*dynWidth->value/2.0, 0.0, dynWidth->value, dynHeight->value );

}

QRectF leftXCenterYRect()
{
    return QRectF(0.0, -1.0*dynHeight->value/2.0, dynWidth->value, dynHeight->value );
}

QRectF leftXTopYRect()
{
    return QRectF(0.0, 0.0, dynWidth->value, dynHeight->value );

}
